package com.ry.framework.shiro.session;

import java.io.Serializable;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.UnknownSessionException;
import org.apache.shiro.session.mgt.SessionKey;
import org.apache.shiro.web.servlet.ShiroHttpServletRequest;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.apache.shiro.web.session.mgt.WebSessionKey;
import org.apache.shiro.web.util.WebUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import com.ry.common.utils.CheckMobile;

/**
 * 自定义 session管理器
 * @author Szj
 *
 */
public class MySessionManager extends DefaultWebSessionManager {

	private static final Logger log = LoggerFactory.getLogger(MySessionManager.class);

	private static final String AUTHORIZATION = "token";

	private static final String REFERENCED_SESSION_ID_SOURCE = "Stateless request";

	public MySessionManager() {
		super();
		//设置超时时间，默认1800000,30min
		setGlobalSessionTimeout(DEFAULT_GLOBAL_SESSION_TIMEOUT * 48);
	}

	@Override
	protected Serializable getSessionId(ServletRequest request, ServletResponse response) {
		String userAgent = WebUtils.toHttp(request).getHeader( "USER-AGENT" ).toLowerCase();
		if(null == userAgent){  
		    userAgent = "";  
		}
		boolean isFromMobile = CheckMobile.check(userAgent);
		//判断是否为移动端访问
		String ua = "pc";
		if(isFromMobile){
			System.out.println("移动端访问");
			ua = "mobile";
		} 
		request.setAttribute("ua",ua);
		
		String id = WebUtils.toHttp(request).getHeader(AUTHORIZATION);
		// 如果请求头中有 Authorization 则其值为sessionId
		if (!StringUtils.isEmpty(id)) {
//			id += ua;
			log.info("请求头中获取");
			request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_SOURCE, REFERENCED_SESSION_ID_SOURCE);
			request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID, id);
			request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_IS_VALID, Boolean.TRUE);
		} else {
			log.info("默认方式获取sessionId");
			// 否则按默认规则从cookie取sessionId
			id = (String) super.getSessionId(request, response);
//			id += ua;
		}
		
		return id;
	}

	/**
	 * 处理频繁访问redis问题
	 */
	@Override
	protected Session retrieveSession(SessionKey sessionKey) throws UnknownSessionException {
		// TODO Auto-generated method stub
		Session session = null;
		Serializable sessionId = sessionKey.getSessionId();
		ServletRequest request = null;
		if(sessionKey instanceof WebSessionKey) {
			request = ((WebSessionKey) sessionKey).getServletRequest();
		}
		if(request != null && sessionId != null) {
			session = (Session) request.getAttribute(sessionId.toString());
			if(session != null) {
				return session;
			}
		}
		session = super.retrieveSession(sessionKey);
		if(request != null && sessionId != null) {
			request.setAttribute(sessionId.toString(), session);
		}
		
		return session;
	}
	
}
